package com.wenx.v3system.modular.platform.controller;

import com.wenx.v3core.response.R;
import com.wenx.v3secure.annotation.RequiresPermissions;
import com.wenx.v3secure.enums.PlatformPermission;
import com.wenx.v3system.modular.platform.service.PlatformMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.wenx.v3secure.annotation.RequiresPermissions.Logical.OR;

/**
 * 平台菜单管理控制器
 *
 * @author wenx
 */
@Slf4j
@RestController
@RequestMapping("/api/v3/platform/menus")
@Tag(name = "平台菜单管理", description = "平台菜单相关接口")
@RequiredArgsConstructor
public class PlatformMenuRestController {

    private final PlatformMenuService platformMenuService;

    /**
     * 获取所有平台菜单
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有平台菜单", description = "获取所有平台菜单列表")
    @RequiresPermissions(value = {PlatformPermission.PLATFORM_ALL_CODE, PlatformPermission.PLATFORM_ROLE_READ_CODE}, logical = OR)
    public R getAllMenus() {
        List<Map<String, Object>> menus = platformMenuService.getAllMenusAsMap();
        return R.ok(menus);
    }

    /**
     * 获取菜单分组
     */
    @GetMapping("/groups")
    @Operation(summary = "获取菜单分组", description = "获取菜单按分组分类的结果")
    @RequiresPermissions(value = {PlatformPermission.PLATFORM_ALL_CODE, PlatformPermission.PLATFORM_ROLE_READ_CODE}, logical = OR)
    public R getMenuGroups() {
        Map<String, List<Map<String, Object>>> menuGroups = platformMenuService.getMenuGroupsAsMap();
        return R.ok(menuGroups);
    }

    /**
     * 获取菜单权限码列表
     */
    @GetMapping("/permission-codes")
    @Operation(summary = "获取菜单权限码", description = "获取所有菜单的权限码列表")
    @RequiresPermissions(value = {PlatformPermission.PLATFORM_ALL_CODE, PlatformPermission.PLATFORM_ROLE_READ_CODE}, logical = OR)
    public R getMenuPermissionCodes() {
        List<String> permissionCodes = platformMenuService.getAllMenuPermissionCodes();
        return R.ok(permissionCodes);
    }

}